package com.ztjc.day02_sdsize;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.text.format.Formatter;
import android.widget.TextView;

/**
 * 获取sd卡的信息
 */
public class MainActivity extends Activity {

    private TextView tvSdSize;

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvSdSize = (TextView) findViewById(R.id.tv_sd_size);

        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getPath());

            long blockCountLong = statFs.getBlockCountLong();
            long blockSizeLong = statFs.getBlockSizeLong();
            long availableBlocksLong = statFs.getAvailableBlocksLong();

            tvSdSize.setText("区块大小:" + blockSizeLong + "总空间:" + Formatter.formatFileSize(this, blockSizeLong * blockCountLong) + "可用空间:" + Formatter.formatFileSize(this, availableBlocksLong * blockSizeLong));
        }
    }

}
